package com.xiedafeng.www.util;

import javax.sql.DataSource;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UpdateUtil<T> {
    private static final int NUMBERAFTERWHERE= 1;
    /*
    单表update数据，condition是条件（目标行）（不通过主键id），即sql语句中where部分，target是希望更改的地方（目标列），即set部分
     */
    public int update(T t,String[] condition,String[] target) throws Exception {

        StringBuilder sql= new StringBuilder("update " + t.getClass().getSimpleName() + " set ");
        DataSource dataSource=new DataSourceUtil();
        Connection connection=null;
        PreparedStatement preparedStatement = null;
        int result=0;
        try {
            connection = dataSource.getConnection();
            if (target.length > 0) {
                sql.append(target[0]).append(" = ?");
                for (int i = 1; i < target.length; i++) {
                    sql.append(", ").append(target[i]).append(" = ?");
                }
            }

            if (condition.length > 0) {
                sql.append(" where ").append(condition[0]).append(" = ?");
                for (int i = 1; i < condition.length; i++) {
                    sql.append(" and ").append(condition[i]).append(" = ?");
                }
            }
            preparedStatement = connection.prepareStatement(sql.toString());
            for (int i = 0; i < target.length; i++) {
                Method method = t.getClass().getMethod("get" + target[i]);
                preparedStatement.setObject(i + 1, method.invoke(t));
            }
            for (int i = 0; i < condition.length; i++) {
                Method method = t.getClass().getMethod("get" + condition[i]);
                preparedStatement.setObject(i + 1 + target.length, method.invoke(t));
            }
            result=preparedStatement.executeUpdate();
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }
        finally {
            if (connection != null) {
                connection.close();
            }
            JdbcUtil.close(preparedStatement);
        }
        return result;
    }
    /*
    通过主键id直接锁定目标行（一行），target同样表示目标列
     */
    public int update(T t,int id,String[] target) throws Exception {
        StringBuilder sql= new StringBuilder("update " + t.getClass().getSimpleName() + " set ");
        DataSource dataSource=new DataSourceUtil();
        Connection connection=null;
        PreparedStatement preparedStatement = null;
        int result=0;
        try {
            connection = dataSource.getConnection();
            if (target.length > 0) {
                sql.append(target[0]).append(" = ?");
                for (int i = 1; i < target.length; i++) {
                    sql.append(", ").append(target[i]).append(" = ?");
                }
            }
            sql.append(" where id = ?");
            preparedStatement = connection.prepareStatement(sql.toString());
            for (int i = 0; i < target.length; i++) {
                Method method = t.getClass().getMethod("get" + target[i]);
                preparedStatement.setObject(i + NUMBERAFTERWHERE, method.invoke(t));
            }
            preparedStatement.setObject(target.length+NUMBERAFTERWHERE,id);
            result=preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        finally {
            if (connection != null) {
                connection.close();
            }
            JdbcUtil.close(preparedStatement);
        }
        return result;
    }
}
